package mapReduce.writable;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class ProductOrderWritable implements WritableComparable<ProductOrderWritable> {

    private String orderId = "";
    private String productId = "";
    private int productCount = 0;
    private String productName = "";
    private String flag = "";

    @Override
    public int compareTo(ProductOrderWritable o) {
        String thisOrderId = orderId;
        String thatOrderId = o.orderId;

        String thisProductId = productId;
        String thatProductId = o.productId;

        int thisProductCount = productCount;
        int thatProductCount = o.productCount;

        String thisProductName = productName;
        String thatProductName = o.productName;

        if(thisProductId.equals(thatProductId)){
            if(thisOrderId.equals(thatOrderId)){
                if(thisProductCount == thatProductCount){
                    return thisProductName.compareTo(thatProductName);
                }else{
                    return thisProductCount-thatProductCount;
                }
            }else{
                return thisOrderId.compareTo(thatOrderId);
            }
        }else{
            return thisProductId.compareTo(thatProductId);
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(orderId);
        out.writeUTF(productId);
        out.writeInt(productCount);
        out.writeUTF(productName);
        out.writeUTF(flag);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.orderId = in.readUTF();
        this.productId = in.readUTF();
        this.productCount = in.readInt();
        this.productName = in.readUTF();
        this.flag = in.readUTF();
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public int getProductCount() {
        return productCount;
    }

    public void setProductCount(int productCount) {
        this.productCount = productCount;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @Override
    public String toString() {
        return orderId + "\t" + productId + "\t" + productName + "\t" + productCount;
    }
}
